package akka.greeting;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;

/**
 * akka:
 * 1.construct the Actor
 * 2.define message
 *
 * Created by Arnold on 2018/8/7.
 */
public class Greeter extends AbstractActor {

  private String message;
  private ActorRef printerActor;
  private String greeting = "";

  // construct the Actor
  public static Props props(String message, ActorRef printerActor){
    return Props.create(Greeter.class, () -> new Greeter(message, printerActor));
  }

  // define message
  static public class WhoToGreet {
    public final String who;

    public WhoToGreet(String who) {
      this.who = who;
    }
  }

  static public class Greet {
    public Greet() {
    }
  }

  @Override
  public Receive createReceive() {
    return receiveBuilder()
      .match(WhoToGreet.class, wtg -> {
        this.greeting = message + ", " + wtg.who;
      })
      .match(Greet.class, x -> {
        printerActor.tell(greeting, getSelf());
      })
      .build();
  }

  public Greeter(String message, ActorRef printerActor){
    this.message = message;
    this.printerActor = printerActor;
  }

}
